package org.social.db.bean;

import com.sojoys.artifact.factory.bytebuff.IByteBuff;
import org.social.db.cache.ActivityCache;
import org.apache.commons.lang3.ArrayUtils;
import com.sojoys.artifact.build.data.base.BasePojo;
import java.sql.SQLException;
import com.sojoys.artifact.tools.ToolMap;
import com.sojoys.artifact.tools.SK_Plus;
import org.social.db.cache.ActivityEntryCache;
import com.sojoys.artifact.tools.ToolCopy;
import static org.social.db.StaticResource.ActivityEntryResource.*;
import java.sql.ResultSet;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import static org.social.db.StaticResource.ActivityEntryResource.*;
/**
 * 活动条目
 */
public class ActivityEntry extends BasePojo<ActivityEntry> {
	
	
	/**
	 * 索引值
	 */
	private String[] indexValues = new String[2];
	
	/**
	* id
	*/
	private int id;
	
	/**
	* 所属活动
	*/
	private int activityId;
	
	/**
	* 是否激活
	*/
	private boolean active;
	
	/**
	* 完成条件ID
	*/
	private int finishCondId;
	
	/**
	* 完成条件参数
	*/
	private Map finishParams;
	
	/**
	* 进度
	*/
	private int progress;
	
	/**
	* 奖励
	*/
	private Map reward;
	
	/**
	* 扩展数据(一般只有全服活动会使用)
	*/
	private Map extend;
	
	/**
	* 备注
	*/
	private Map remark;
	
	/**
	* 顺序
	*/
	private int rank;
	
	/**
	* create_date
	*/
	private java.util.Date createDate;
	
	/**
	* modified_date
	*/
	private java.util.Date modifiedDate;
	
	
	@Deprecated
	public static ActivityEntry builder() {
		ActivityEntry activityEntry = new ActivityEntry();
		return activityEntry;
	}
	
	@Deprecated
	public ActivityEntry() {
		super();
	}
	
	public ActivityEntry(int id, int activityId, boolean active, int finishCondId, Map finishParams, int progress, Map reward, Map extend, Map remark, int rank) {
		super();
		this.id = id;
		this.activityId = activityId;
		this.active = active;
		this.finishCondId = finishCondId;
		this.finishParams = finishParams;
		this.progress = progress;
		this.reward = reward;
		this.extend = extend;
		this.remark = remark;
		this.rank = rank;
		this.createDate = new java.util.Date();
		this.modifiedDate = new java.util.Date();
	}
	
	/**
	 * id
	 */
	public int getId() {return id;}
	
	/**
	 * id
	 */
	public ActivityEntry setId(int id) {this.id = id;return this;}
	
	/**
	 * 所属活动
	 */
	public int getActivityId() {return activityId;}
	
	/**
	 * 所属活动
	 */
	public ActivityEntry setActivityId(int activityId) {this.activityId = activityId;return this;}
	
	/**
	 * 是否激活
	 */
	public boolean isActive() {return active;}
	
	/**
	 * 是否激活
	 */
	public ActivityEntry setActive(boolean active) {this.active = active;return this;}
	
	/**
	 * 完成条件ID
	 */
	public int getFinishCondId() {return finishCondId;}
	
	/**
	 * 完成条件ID
	 */
	public ActivityEntry setFinishCondId(int finishCondId) {this.finishCondId = finishCondId;return this;}
	
	/**
	 * 完成条件参数
	 */
	public Map getFinishParams() {return finishParams;}
	
	/**
	 * 完成条件参数
	 */
	public ActivityEntry setFinishParams(Map finishParams) {this.finishParams = finishParams;return this;}
	
	/**
	 * 完成条件参数
	 */
	public String getFinishParamsString() {
	    return toJSONString(finishParams);
	}
	
	/**
	 * 完成条件参数
	 */
	public Map initFinishParamsString(String str) {
		return forJsonObject(str,Map.class);
	}	
	/**
	 * 进度
	 */
	public int getProgress() {return progress;}
	
	/**
	 * 进度
	 */
	public ActivityEntry setProgress(int progress) {this.progress = progress;return this;}
	
	/**
	 * 奖励
	 */
	public Map getReward() {return reward;}
	
	/**
	 * 奖励
	 */
	public ActivityEntry setReward(Map reward) {this.reward = reward;return this;}
	
	/**
	 * 奖励
	 */
	public String getRewardString() {
	    return toJSONString(reward);
	}
	
	/**
	 * 奖励
	 */
	public Map initRewardString(String str) {
		return forJsonObject(str,Map.class);
	}	
	/**
	 * 扩展数据(一般只有全服活动会使用)
	 */
	public Map getExtend() {return extend;}
	
	/**
	 * 扩展数据(一般只有全服活动会使用)
	 */
	public ActivityEntry setExtend(Map extend) {this.extend = extend;return this;}
	
	/**
	 * 扩展数据(一般只有全服活动会使用)
	 */
	public byte[] getExtendByte() {
		return toBytes(extend);
	}
	
	/**
	 * 扩展数据(一般只有全服活动会使用)
	 */
	public Map initExtendByte(byte[] _byte) {
		return (Map)forBytesObject(_byte);
	}	
	/**
	 * 备注
	 */
	public Map getRemark() {return remark;}
	
	/**
	 * 备注
	 */
	public ActivityEntry setRemark(Map remark) {this.remark = remark;return this;}
	
	/**
	 * 备注
	 */
	public String getRemarkString() {
	    return toJSONString(remark);
	}
	
	/**
	 * 备注
	 */
	public Map initRemarkString(String str) {
		return forJsonObject(str,Map.class);
	}	
	/**
	 * 顺序
	 */
	public int getRank() {return rank;}
	
	/**
	 * 顺序
	 */
	public ActivityEntry setRank(int rank) {this.rank = rank;return this;}
	
	/**
	 * create_date
	 */
	public java.util.Date getCreateDate() {return createDate;}
	
	/**
	 * create_date
	 */
	protected ActivityEntry setCreateDate(java.util.Date createDate) {this.createDate = createDate;return this;}
	
	/**
	 * modified_date
	 */
	public java.util.Date getModifiedDate() {return modifiedDate;}
	
	/**
	 * modified_date
	 */
	protected ActivityEntry setModifiedDate(java.util.Date modifiedDate) {this.modifiedDate = modifiedDate;return this;}
	
	
	// 代理
	
	protected Object[] getColumnNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return SOURCE_COLUMN_NAMES;
		case PINGYING:
			return PINGYING_COLUMN_NAMES;
		case HASHCODE:
			return HASHCODE_COLUMN_NAMES;
		case REMARKS:
			return SOURCE_COLUMN_REMARKS;
		default:
			return PINGYING_COLUMN_NAMES;
		}
	}
	
	protected Object[] getFKTableNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return FK_SOURCE_TABLE_NAMES;
		case PINGYING:
			return FK_PINGYING_TABLE_NAMES;
		case HASHCODE:
			return FK_HASHCODE_TABLE_NAMES;
		default:
			return FK_PINGYING_TABLE_NAMES;
		}
	}
	
	protected Object[] getFKColumnNames(DataModelKeyEnum e){
		switch (e) {
		case SOURCE:
			return FK_SOURCE_COLUMN_NAMES;
		case PINGYING:
			return FK_PINGYING_COLUMN_NAMES;
		case HASHCODE:
			return FK_HASHCODE_COLUMN_NAMES;
		default:
			return FK_PINGYING_TABLE_NAMES;
		}
	}
	
	protected String[] getBasicTypes(){
		return COLUMN_BASICTYPES;
	}
	
	protected Class[] getClassTypes(){
		return COLUMN_CLASSTYPES;
	}
	
	protected String[] getColumnTypes(){
		return COLUMN_COLUMNTYPES;
	}
	
	protected int getColumnCount(){
		return COLUMN_COUNT;
	}
	
	protected Object[] getColumeValues(){
		Object[] values = new Object[COLUMN_COUNT];
        values[0] = this.id;
        values[1] = this.activityId;
        values[2] = this.active;
        values[3] = this.finishCondId;
        values[4] = this.finishParams;
        values[5] = this.progress;
        values[6] = this.reward;
        values[7] = this.extend;
        values[8] = this.remark;
        values[9] = this.rank;
        values[10] = this.createDate;
        values[11] = this.modifiedDate;
        return values;
	}
	
	public ActivityEntry deepCopy(){
		ActivityEntry activityEntry = null;
		try {
			activityEntry = (ActivityEntry) super.clone();
		    activityEntry.setFinishParams(ToolCopy.copyObject(this.finishParams));
		    activityEntry.setReward(ToolCopy.copyObject(this.reward));
		    activityEntry.setExtend(ToolCopy.copyObject(this.extend));
		    activityEntry.setRemark(ToolCopy.copyObject(this.remark));
		    activityEntry.setCreateDate(ToolCopy.copyObject(this.createDate));
		    activityEntry.setModifiedDate(ToolCopy.copyObject(this.modifiedDate));
		    // 克隆索引
		    activityEntry.indexValues = ArrayUtils.clone(indexValues);
			return activityEntry;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return activityEntry;
	}
	
	
	protected Map<Object, Object> toMap(Object[] keys,Map<Object, Object> map){
        map.put(keys[0], this.id);
        map.put(keys[1], this.activityId);
        map.put(keys[2], this.active);
        map.put(keys[3], this.finishCondId);
        map.put(keys[4], this.finishParams);
        map.put(keys[5], this.progress);
        map.put(keys[6], this.reward);
        map.put(keys[7], this.extend);
        map.put(keys[8], this.remark);
        map.put(keys[9], this.rank);
        map.put(keys[10], this.createDate);
        map.put(keys[11], this.modifiedDate);
        return map;
    }
    
    protected ActivityEntry createForMap(Object[] keys,Map map){
   		this.id = (int)map.get(keys[0]);
   		this.activityId = (int)map.get(keys[1]);
   		this.active = (boolean)map.get(keys[2]);
   		this.finishCondId = (int)map.get(keys[3]);
   		this.finishParams = (Map)map.get(keys[4]);
   		this.progress = (int)map.get(keys[5]);
   		this.reward = (Map)map.get(keys[6]);
   		this.extend = (Map)map.get(keys[7]);
   		this.remark = (Map)map.get(keys[8]);
   		this.rank = (int)map.get(keys[9]);
   		this.createDate = (java.util.Date)map.get(keys[10]);
   		this.modifiedDate = (java.util.Date)map.get(keys[11]);
        return this;
    }
	
	
	protected JSONObject toJSONObject(Object[] keys,JSONObject obj){
        obj.put(keys[0], this.id);
        obj.put(keys[1], this.activityId);
        obj.put(keys[2], this.active);
        obj.put(keys[3], this.finishCondId);
        obj.put(keys[4], this.finishParams);
        obj.put(keys[5], this.progress);
        obj.put(keys[6], this.reward);
        obj.put(keys[7], toBytes(extend));
        obj.put(keys[8], this.remark);
        obj.put(keys[9], this.rank);
        obj.put(keys[10], this.createDate);
        obj.put(keys[11], this.modifiedDate);
        return obj;
    }
    
    protected ActivityEntry createForJSONObject(Object[] keys,JSONObject obj){
	    this.id = obj.getIntValue(keys[0]);
	    this.activityId = obj.getIntValue(keys[1]);
	    this.active = obj.getBooleanValue(keys[2]);
	    this.finishCondId = obj.getIntValue(keys[3]);
		this.finishParams = obj.getObject(keys[4],Map.class);
	    this.progress = obj.getIntValue(keys[5]);
		this.reward = obj.getObject(keys[6],Map.class);
		this.extend = (Map)forBytesObject(obj.getBytes(keys[7]));
		this.remark = obj.getObject(keys[8],Map.class);
	    this.rank = obj.getIntValue(keys[9]);
	    this.createDate = obj.getDate(keys[10]);
	    this.modifiedDate = obj.getDate(keys[11]);
        return this;
    }
    
    
    public ActivityEntry createForResultSet(ResultSet rs) throws SQLException{
	    this.id = rs.getInt(SOURCE_COLUMN_NAMES[0]);
	    this.activityId = rs.getInt(SOURCE_COLUMN_NAMES[1]);
	    this.active = rs.getBoolean(SOURCE_COLUMN_NAMES[2]);
	    this.finishCondId = rs.getInt(SOURCE_COLUMN_NAMES[3]);
		this.finishParams = forJsonObject(rs.getString(SOURCE_COLUMN_NAMES[4]),Map.class);
	    this.progress = rs.getInt(SOURCE_COLUMN_NAMES[5]);
		this.reward = forJsonObject(rs.getString(SOURCE_COLUMN_NAMES[6]),Map.class);
		this.extend = (Map)forBytesObject(rs.getBytes(SOURCE_COLUMN_NAMES[7]));
		this.remark = forJsonObject(rs.getString(SOURCE_COLUMN_NAMES[8]),Map.class);
	    this.rank = rs.getInt(SOURCE_COLUMN_NAMES[9]);
	    this.createDate = rs.getTimestamp(SOURCE_COLUMN_NAMES[10]);
	    this.modifiedDate = rs.getTimestamp(SOURCE_COLUMN_NAMES[11]);
	    return this;
    }
    
    protected void writeBytes(IByteBuff byteBuf){
	    byteBuf.writeInt(this.id);
	    byteBuf.writeInt(this.activityId);
	    byteBuf.writeBoolean(this.active);
	    byteBuf.writeInt(this.finishCondId);
	    byteBuf.writeObject(this.finishParams);
	    byteBuf.writeInt(this.progress);
	    byteBuf.writeObject(this.reward);
	    byteBuf.writeObject(this.extend);
	    byteBuf.writeObject(this.remark);
	    byteBuf.writeInt(this.rank);
	    byteBuf.writeDate(this.createDate);
	    byteBuf.writeDate(this.modifiedDate);
    }
	
	protected ActivityEntry readBytes(IByteBuff byteBuf){
	    this.id = byteBuf.readInt(null);
	    this.activityId = byteBuf.readInt(null);
	    this.active = byteBuf.readBoolean(null);
	    this.finishCondId = byteBuf.readInt(null);
	    this.finishParams = (Map)byteBuf.readObject();
	    this.progress = byteBuf.readInt(null);
	    this.reward = (Map)byteBuf.readObject();
	    this.extend = (Map)byteBuf.readObject();
	    this.remark = (Map)byteBuf.readObject();
	    this.rank = byteBuf.readInt(null);
	    this.createDate = byteBuf.readDate(null);
	    this.modifiedDate = byteBuf.readDate(null);
	    return this;
	}
    
    
    protected JSONObject exportFKTableMap(Object[] keys,JSONObject map,DataModelKeyEnum e){
        return map;
    }
    
    protected boolean importFKTableMap(Object[] keys,JSONObject map,DataModelKeyEnum e){
		return true;
    }
    
	/** 取得[activity]*/
	public Activity getActivityPkActivityId(){
		return ActivityCache.me().getById(activityId);
	}
	
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public ActivityEntry insert(){
    	return ActivityEntryCache.me().insert(this);
    }
    /** 延迟更新数据库 */
    public ActivityEntry update(){
    	return ActivityEntryCache.me().update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return ActivityEntryCache.me().delete(this);
    }
    /** 即时插入数据库 */
    public ActivityEntry insertAndFlush(){
    	return ActivityEntryCache.me().insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public ActivityEntry updateAndFlush(){
    	return ActivityEntryCache.me().updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return ActivityEntryCache.me().deleteAndFlush(this);
    }
    
    
	
	/**
	 * 同步索引值
	 */
	public ActivityEntry syncIndexValues(){
		indexValues[0] = SK_Plus.b(getId()).e();
		indexValues[1] = SK_Plus.b(getActivityId()).e();
		return this;
	}
	/**
	 * 取得索引
	 */
	public String[] getIndexValues(){
		return indexValues;
	}
	
	protected Object getPrimaryKey(){
		return getId();
	}
	
	protected void setPrimaryKey(Object key){
		setId((int)key);
	}
//自定义内容起始位置
//自定义内容结束位置
}